package cn.liulin.java8.stream;

import cn.liulin.java8.pojo.Dish;
import com.alibaba.fastjson.JSON;

import java.util.*;
import java.util.stream.Collectors;

/**
 * cn.liulin.java8.stream$
 *
 * @author ll
 * @date 2021-10-15 16:12:35
 **/
public class MoreGroupStream {
    public static void main(String[] args) {
        Dish dish1 = new Dish("hasd", 100);
        Dish dish2 = new Dish("g", 200);
        Dish dish3 = new Dish("asdf", 300);
        Dish dish6 = new Dish("asdf", 100);
        Dish dish4 = new Dish("dfg", 400);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4, dish6);
//        groupStreamOne(menu);
//        groupStreamTwo(menu);
//        groupStreamThree(menu);
//        groupStreamFour(menu);
//        groupStreamFive(menu);
        groupStreamSix(menu);
    }

    private static void groupStreamSix(List<Dish> menu) {
        Map<String, HashSet<Integer>> collect = menu.stream()
                .collect(
                        Collectors.groupingBy(Dish::getName, Collectors.mapping(
                                dish -> {
                                    if (dish.getCalories() > 200) {
                                        return 1;
                                    } else {
                                        return 2;
                                    }
                                }, Collectors.toCollection(HashSet::new)
                        ))
                );
        System.out.println(JSON.toJSONString(collect));
    }

    private static void groupStreamFive(List<Dish> menu) {
        Map<String, Set<String>> collect = menu.stream()
                .collect(
                        Collectors.groupingBy(Dish::getName, Collectors.mapping(
                                dish -> {
                                    if (dish.getCalories() > 200) {
                                        return "1";
                                    } else {
                                        return "2";
                                    }
                                }, Collectors.toSet()
                        ))
                );
        System.out.println(JSON.toJSONString(collect));
    }

    private static void groupStreamFour(List<Dish> menu) {
        Map<String, Dish> collect = menu.stream()
                .collect(
                        //分类函数
                        Collectors.groupingBy(Dish::getName, Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)), Optional::get //包装后的收集器
                        ))
                );
        System.out.println(JSON.toJSONString(collect));
    }

    private static void groupStreamThree(List<Dish> menu) {
        Map<String, Optional<Dish>> collect = menu.stream()
                .collect(
                        Collectors.groupingBy(Dish::getName, Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)))
                    );
        System.out.println(JSON.toJSONString(collect));
    }

    private static void groupStreamTwo(List<Dish> menu) {
        Map<String, Long> collect = menu.stream()
                .collect(
                        Collectors.groupingBy(Dish::getName, Collectors.counting())
                );
        System.out.println(JSON.toJSONString(collect));
    }

    private static void groupStreamOne(List<Dish> menu) {
        Map<String, Map<String, List<Dish>>> collect = menu.stream()
                .collect(
                        //二级分类函数
                        Collectors.groupingBy(Dish::getName, Collectors.groupingBy(
                                dish -> {
                                    if (dish.getCalories() > 300) {
                                        return "肥";
                                    } else if (dish.getCalories() > 100) {
                                        return "中";
                                    } else {
                                        return "瘦";
                                    }
                                }
                        ))
                );
        System.out.println(JSON.toJSONString(collect));
    }

}
